﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using Library.UI.Form;
using QuanLyDiem.Controllers;
using QuanLyDiem.models;
namespace QuanLyDiem.ThongKe
{
    public partial class frmThongKeDiemSinhVien : DevExpress.XtraEditors.XtraForm
    {
        public frmThongKeDiemSinhVien()
        {
            InitializeComponent();
        }

        private void frmThongKeDiemSinhVien_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            gridView1.Config(true, false, false, false, false, false, true, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            gridView2.Config(true, false, false, false, false, false, true, false, false);
            gridView2.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            //C_Mon.LoadDataSource(rleMonHoc, db);
            
            var ID_lops = db.LopHocPhans.Select(o => o.ID_lop).Distinct().ToList();
            var lops = new List<Lop>();
            foreach (var row in ID_lops)
            {
                var lop = db.Lops.Where(p => p.ID_lop == row).First();
                lops.Add(lop);
            }
            cmbLop.DataSource = lops.ToList();
            cmbLop.DisplayMember = "Ten_lop";
            cmbLop.ValueMember = "ID_lop";
            var namhocs = db.LopHocPhans.Select(o => o.Nam_hoc).Distinct().ToList();
            var hockys = db.LopHocPhans.Select(o => o.Hoc_ky).Distinct().ToList();
            cmbNamHoc.DataSource = namhocs.ToList();
            cmbHocKy.DataSource = hockys.ToList();
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            Context db = new Context();
            var ID_lop_hps = db.LopHocPhans.Where(p => p.ID_lop == int.Parse(cmbLop.SelectedValue.ToString()) && p.Nam_hoc == cmbNamHoc.Text && p.Hoc_ky == p.Hoc_ky).Select(o => o.ID_lop_hp).FirstOrDefault();
            if (ID_lop_hps != 0)
            {
                var lophps = db.Diems.Where(p => p.ID_lop_hp == ID_lop_hps).ToList();
                var Listlhp = new List<EXT_Diem>();
                foreach (var row in lophps)
                {
                    var sinhvien = db.HoSoSinhViens.Where(p => p.ID_sv == row.ID_sv).Select(o => new { o.Ho_ten, o.Ma_sv, o.Ngay_sinh }).First();
                    var ext_diem = new EXT_Diem
                    {
                        Ten_sinh_vien = sinhvien.Ho_ten,
                        Ma_sv = int.Parse(sinhvien.Ma_sv),
                        Ngay_sinh = sinhvien.Ngay_sinh.ToString().Substring(0, sinhvien.Ngay_sinh.ToString().IndexOf(" ")),
                        ID_sv = row.ID_sv,
                    };
                    Listlhp.Add(ext_diem);
                }
                eXTDiemBindingSource.DataSource = Listlhp.ToList();
            }
            else
                MessageBox.Show("Không có dữ liệu");
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Context db = new Context();
            var row = gridView1.GetFocusedRow() as EXT_Diem;
            var ID_lop_hps = db.LopHocPhans.Where(p => p.ID_lop == int.Parse(cmbLop.SelectedValue.ToString()) && p.Nam_hoc == cmbNamHoc.Text && p.Hoc_ky == p.Hoc_ky).ToList();
            var list =new List<EXT_DiemSinhVien>();
            foreach (var value in ID_lop_hps)
            {
                var value1 = db.Diems.Where(p => p.ID_sv == row.ID_sv && p.ID_lop_hp == value.ID_lop_hp).FirstOrDefault();
                if (value1 != null)
                {
                    var tenmon = db.MonHocs.Where(p => p.ID_mon == value.ID_mon).Select(o => o.Ten_mon).First();
                    var diem = new EXT_DiemSinhVien 
                    {
                        ID_sv=row.ID_sv,
                        ID_lop_hp=value.ID_lop_hp,
                        Diem_X=value1.Diem_X,
                        Diem_Y=value1.Diem_Y,
                        Diem_Z=value1.Diem_Z,
                        Ten_mon=tenmon
                    };
                    list.Add(diem);
                }
            }
            eXTDiemSinhVienBindingSource.DataSource = list.ToList();
        }

        private void gridControl2_Click(object sender, EventArgs e)
        {

        }
    }
}